package com.qingcloud.fexecutor.base.handler;

import cn.hutool.db.nosql.mongo.MongoDS;
import cn.hutool.db.nosql.redis.RedisDS;
import cn.hutool.setting.Setting;
import com.mongodb.client.MongoClient;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.annotation.MagicModule;
import org.ssssssss.script.annotation.Comment;
import redis.clients.jedis.Jedis;

/**
 * 轻云redis模块
 */
@Component
@MagicModule("qingCloud")
public class QingCloudModule {

    /**
     * 获取Jedis客户端实例
     */
    @Comment("获取Jedis客户端实例")
    public Jedis getJedis(@Comment("redis地址") String host,
                          @Comment("redis端口") Integer port,
                          @Comment("redis密码") String password,
                          @Comment("redis数据库") Integer database) {
        Setting setting = new Setting();
        setting.put("host", host);
        setting.put("port", String.valueOf(port));
        setting.put("password", password);
        setting.put("database", String.valueOf(database));
        RedisDS redisDS = RedisDS.create(setting, null);
        return redisDS.getJedis();
    }

    /**
     * 获取mongodb客户端实例
     */
    @Comment("获取Jedis客户端实例")
    public MongoClient getMongo(@Comment("mongo地址") String host,
                                @Comment("mongo端口") Integer port,
                                @Comment("mongo用户名") String user,
                                @Comment("mongo密码") String password,
                                @Comment("mongo数据库") String database) {
        Setting setting = new Setting();
        setting.put("user", user);
        setting.put("pass", password);
        setting.put("database", database);
        MongoDS mongoDS = new MongoDS(setting, host, port);
        return mongoDS.getMongo();
    }

}